package cn.zhibang.sannuo.web.controller;

import cn.zhibang.sannuo.account.service.AAccountnumderService;
import cn.zhibang.sannuo.account.service.ARoleService;
import cn.zhibang.sannuo.commons.model.Accountnumber;
import cn.zhibang.sannuo.commons.model.BranchOffice;
import cn.zhibang.sannuo.commons.model.HeadOffice;
import cn.zhibang.sannuo.commons.model.Role;
import cn.zhibang.sannuo.pharmacy.service.PBranchOfficeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author AdminController
 * @version 1.0.0
 * @ClassName AccountController.java
 * @Description TODO 系统账号管理
 * @createTime 2021年03月29日 15:16:00
 */
@Controller
@RequestMapping("/account")
public class AccountController {
    @Resource private PBranchOfficeService pBranchOfficeService;
    @Resource private ARoleService aRoleService;
    @Resource private AAccountnumderService aAccountnumderService;

    @GetMapping("/account_list")//系统账号列表
    public String account_list(){
        return "html/account_list";
    }

    @GetMapping("/account_add")
    public String account_add(Model model){
        Subject subject = SecurityUtils.getSubject();
        Accountnumber accountnumber = (Accountnumber)subject.getPrincipal();
        Integer hid;
        if (accountnumber.getBranchOffice() == null){
            hid = null;
        }else {
            hid = accountnumber.getBranchOffice().getHeadOffice().getHid();
        }
        List<BranchOffice> branchOffices = pBranchOfficeService.selAllBranchOffice(hid, null, null, null, null);
        String rnumber = aAccountnumderService.selAccountnumberRnumber(accountnumber.getAccount());
        String rnumbers = "";
        if ("CJ".equals(rnumber)){
            rnumbers = "'PT','FD'";
        }
        if ("ZD".equals(rnumber)){
            rnumbers = "'FD'";
        }
        List<Role> roles = aRoleService.selRoleRnumber(rnumbers);
        model.addAttribute("branchOffices", branchOffices);
        model.addAttribute("roles", roles);
        return "html/account_add";
    }

    @GetMapping("/account_edit")//编辑页面
    public String account_edit(String account, String aname, String phone, String bname, String line1, Model model){
        Accountnumber accountnumber = new Accountnumber();
        accountnumber.setAccount(account);
        accountnumber.setAname(aname);
        accountnumber.setPhone(phone);
        accountnumber.setLine1(line1);
        BranchOffice branchOffice = new BranchOffice();
        branchOffice.setBname(bname);
        accountnumber.setBranchOffice(branchOffice);
        model.addAttribute("accountnumber", accountnumber);
        return "html/account-edit";
    }

    @GetMapping("/account_password")
    public String account_password(String account, Model model){
        model.addAttribute("account", account);
        return "html/account-password";
    }

}
